** This file produces analysis in Table 3.3

use SLERs1967to2010.dta, clear

* Put states that hold elections in odd years on an even number calendar so easily
* comparable with remaining states
gen odd = mod(year,2)
replace year=year+1 if odd==1 & (statabbr=="KY" | statabbr=="LA" | statabbr=="MS" | statabbr=="NJ" | statabbr=="VA")

* Generate an indicator for which chamber a candidate is running for
recode v07 8=2 9=1, gen(chamber)

* Fix MA district names to standardize for later merging
sort statabbr v08 v09
merge statabbr v08 v09 using slers_ma_reconcile
drop if _m==2
drop _m

* Create indicator for a legislator's district
ren v09 dist
replace dist=code if statabbr=="MA"

* Keep only years 1996-2010, best coverage on data 
keep if year>1995 & year<2011

drop if statabbr=="NV" | statabbr=="VT" | statabbr=="AK"  
drop if chamber==1 & (statabbr=="ID" | statabbr=="MD" | statabbr=="MN" | statabbr=="WA" | statabbr=="MA" ) 

* Drop any duplicates in the dataset
duplicates drop year statabbr chamber dist, force

* Sort and save data for merging back in later
sort year statabbr chamber dist
save temp, replace

* Merge in data on campaign finance from NIMSP
use newnimsp, clear

* Create same odd-year election fix as above
gen odd = mod(year,2)
replace year=year+1 if odd==1  & (statabbr=="KY" | statabbr=="LA" | statabbr=="MS" | statabbr=="NJ" | statabbr=="VA")

* Keep elections between 1996 and 2010
keep if year>1995 & year<2011

* Keep only incumbents since only incumbents have ideology scores
keep if incumbency_statusid =="I"


* Merge back in election data from earlier in file 
ren district dist
sort year statabbr chamber dist
merge year statabbr chamber dist using temp
drop if _m<3 
drop _m


* Merge in Shor/McCarty ideology scores
sort statabbr dist year chamber
merge statabbr dist year chamber using npat1996_2008
drop if _m==2
drop _m

* Drop NE b/c nonpartisan, WA because multi-member districts, and NH because difficulty in merging with other data
drop if statabbr=="NE" | statabbr=="WA" | statabbr=="NH"

* Merge in dataset with campaign finance laws
sort statabbr year
merge statabbr year using masterdataset

* Drop independent candidates
drop if party=="I"

* Drop states that have public financing systems
gen pubfundstate=1 if statabbr=="AZ" & year>1998
replace pubfundstate=1 if statabbr=="CT" & year>2006
replace pubfundstate=1 if statabbr=="ME" & year>1998
replace pubfundstate=1 if statabbr=="MN"
recode pubfundstate .=0
drop if pubfundstate==1

tab party, gen(pid)
ren pid1 democrat

* Generate a variable for how far an incumbent's ideology is from mid-point of scale
gen ideo_dist=abs(np_score)

* Estimate seemingly unrelated regression models on where individuals receive funding
* Only run model on those facing some competition (v35<100)
sureg (prop_from_party l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_individuals l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_business l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_unions l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_issue l_party orglims indlims v35 ideo_dist np_score democrat) if v35<100
estout using table2.xls,  unstack cells(b(star fmt(3)) se( fmt(3))) stats(r2) replace level(99)

** NOTE: After it was too late to edit the manuscript, we discovered that the above 
** analysis includes duplicate observations for state legislators. The analysis below
** corrects this issue and the findings do not change in meaningful ways as a result
** of the above mistake.

duplicates drop statabbr chamber dist np_score, force

sureg (prop_from_party l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_individuals l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_business l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_unions l_party orglims indlims v35 ideo_dist np_score democrat) (prop_from_issue l_party orglims indlims v35 ideo_dist np_score democrat) if v35<100
